package com.canyou.archives;


import com.canyou.api.ResponseBO;
import com.canyou.archives.model.dto.*;
import com.canyou.archives.model.vo.PageVO;
import com.canyou.archives.service.ArchivesService;
import com.canyou.utils.StringUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.canyou.api.BaseApiController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import static com.canyou.api.ResponseBO.error;
import static com.canyou.api.ResponseBO.fail;
import static com.canyou.api.ResponseBO.ok;

/**
 * <p>
 * 设备档案 前端控制器
 * </p>
 *
 * @author canyou
 * @since 2021-12-07
 */
@Api(tags = "设备管理")
@ApiSort(2002)
@RestController
@RequestMapping("/archives")
public class ArchivesController extends BaseApiController {
    @Autowired
    private ArchivesService archivesService;
    @ApiOperation(value = "新增编辑设备")
    //@PreAuthorize("hasPerm('role:add','role:edit')")
    @PostMapping("/save")
    public ResponseBO saveOrUpdateArchives(@ApiParam(name = "archivesDTO", type = "ArchivesDTO", value = "设备新增编辑数据", required = true)
                                           @Valid
                                           @NotNull(message = "设备分类信息不能为空")
                                           @RequestBody ArchivesDTO archivesDTO) {
        //调用逻辑
        Boolean b=archivesService.saveOrUpdateArchives(archivesDTO);
        //返回结果给前端
        if(!b){
            return fail("操作失败！");
        }
        return ok("操作成功");
    }

    @ApiOperation(value = "条件分页查询分页")
    //@PreAuthorize("hasPerm('role')")
    @PostMapping("/get_page/{current}/{pageSize}")
    public ResponseBO getPageList(@ApiParam(name = "current",value = "当前页",required = true)
                                  @PathVariable("current")Integer current,
                                  @ApiParam(name = "pageSize",value = "每页数量",required = true)
                                  @PathVariable("pageSize")Integer pageSize,
                                  @RequestBody(required = false) ArchivesPageDTO archivesPageDTO) {
        PageVO page= archivesService.getPageList(current,pageSize,archivesPageDTO);
        return ok(page);
    }

    @ApiOperation(value = "根据设备id获得详细信息")
    @GetMapping("/get_one/{deviceId}")
    public ResponseBO getDeviceById(@ApiParam(name = "deviceId",value = "设备id",required = true)
                                   @PathVariable("deviceId")Long deviceId){
        //调用逻辑层
        ArchivesInformationDTO archivesInformationDTO= archivesService.getDeviceById(deviceId);
        //判断新增完成与否
        if(StringUtils.isNotNull(archivesInformationDTO)){
            //新增成功
            return ok().setMsg("操作成功").setData(archivesInformationDTO);
        }
        return fail("操作失败");
    }

    @ApiOperation(value = "批量处理设备1启用2停用3报废")
    @PutMapping("/change_state")
    public ResponseBO updateDisableEnableDevice(@ApiParam(name = "QueryPlayersVo",value = "条件",required = true)
                                                    @Valid
                                                    @NotNull(message = "设备分类信息不能为空")
                                                @RequestBody ChangeArcvhivesStateDTO dea){
        //调用逻辑层完成启用和禁用设备功能
        boolean b=archivesService.updateDisableenabledevice(dea);
        //判断新增完成与否
        if(b){
            //新增成功
            return ok().setMsg("操作成功");
        }
        return fail("操作失败");
    }

    @ApiOperation(value = "批量处理设备修改为已入账")
    @PutMapping("/change_registration")
    public ResponseBO updateRegistrationDevice(@ApiParam(name = "QueryPlayersVo",value = "条件",required = true)
                                                @Valid
                                                @NotNull(message = "设备分类信息不能为空")
                                                @RequestBody ChangeArcvhivesStateDTO dea){
        //调用逻辑层完成
        boolean b=archivesService.updateRegistrationDevice(dea);
        //判断新增完成与否
        if(b){
            //新增成功
            return ok().setMsg("操作成功");
        }
        return fail("操作失败");
    }

    @ApiOperation(value = "设备领用")
    @PutMapping("/device/receive")
    public ResponseBO receiveDevice(@ApiParam(name = "useArchivesLogsDTO", type = "UseArchivesLogsDTO", value = "设备来记录处理类", required = true)
                                        @Valid
                                        @NotNull(message = "设备信息不能为空")
                                        @RequestBody UseArchivesLogsDTO useArchivesLogsDTO){
        if(StringUtils.isNull(useArchivesLogsDTO.getDeviceId())||StringUtils.isNull(useArchivesLogsDTO.getPersionId())){
            return fail("操作失败");
        }
        //调用逻辑层
        boolean b=archivesService.receiveDevice(useArchivesLogsDTO.getPersionId(),useArchivesLogsDTO.getDeviceId());
        //判断新增完成与否
        if(b){
            //新增成功
            return ok().setMsg("操作成功");
        }
        return fail("操作失败");
    }

    @ApiOperation(value = "设备归还")
    @PutMapping("/device/return")
    public ResponseBO returnDevice(@ApiParam(name = "useArchivesLogsDTO", type = "UseArchivesLogsDTO", value = "设备来记录处理类", required = true)
                                       @Valid
                                       @NotNull(message = "设备信息不能为空")
                                       @RequestBody UseArchivesLogsDTO useArchivesLogsDTO){
        if(StringUtils.isNull(useArchivesLogsDTO.getDeviceId())||StringUtils.isNull(useArchivesLogsDTO.getPersionId())){
            return fail("操作失败");
        }
        //调用逻辑层
        boolean b=archivesService.returnDevice(useArchivesLogsDTO.getPersionId(),useArchivesLogsDTO.getDeviceId());
        //判断新增完成与否
        if(b){
            //新增成功
            return ok().setMsg("操作成功");
        }
        return fail("操作失败");
    }

    @ApiOperation(value = "设备维修")
    @PutMapping("/device/maintain")
    public ResponseBO maintainDevice(@ApiParam(name = "deviceMaintenanceDTO", type = "DeviceMaintenanceDTO", value = "设备维修报备处理类", required = true)
                                         @Valid
                                         @NotNull(message = "设备信息不能为空")
                                         @RequestBody DeviceMaintenanceDTO deviceMaintenanceDTO){
        //调用逻辑层
        boolean b=archivesService.maintainDevice(deviceMaintenanceDTO);
        //判断新增完成与否
        if(b){
            //新增成功
            return ok().setMsg("操作成功");
        }
        return fail("操作失败");
    }

    @ApiOperation(value = "设备基本信息Excel导出")
    //@PreAuthorize("hasPerm('user:export')")
    @GetMapping(value = "/export_excel")
    public ResponseBO exportExcel() {
        Boolean flag= archivesService.exportExcel(this.response);
        if(flag){
            return ok();
        }
        return error();
    }
}
